//
//  DDBossNotificationsViewController.m
//  DingDongZP
//
//  Created by Ray on 16/5/3.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBossNotificationsViewController.h"
#import "DDTitlePullView.h"
#import "DDNotificationMessageCell.h"
#import "DDEmployerNotification.h"
#import "DDJobfinderDetailViewController.h"

@interface DDBossNotificationsViewController ()
<UITableViewDataSource,
UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) DDTitlePullView *pullView;

@property (nonatomic, assign) DDEmployerNotificationType notificationType;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL hasMore;
@property (nonatomic, strong) NSMutableArray *notifications;

@end

@implementation DDBossNotificationsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"全部通知";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 77;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
//    self.titleLabel.attributedText = [self setTitleAttributed];
    self.titleLabel.userInteractionEnabled = YES;
    self.titleLabel.frame = CGRectMake(0, 0, 180, 30);
    self.navigationItem.titleView = self.titleLabel;
    
    self.pullView = [[DDTitlePullView alloc] initWithFrame:self.view.frame];
    //self.titlePullView.backgroundColor = [UIColor clearColor];
    self.pullView.hidden = YES;
    [self.view addSubview:_pullView];
    
    NSArray *titles = @[@"全部通知",@"新加入的牛人",@"查看过我的职位",@"对我的职位感兴趣"];
    self.pullView.titles = titles;
    __weak typeof(self) weakSelf = self;
    self.pullView.seletedTitleBlock = ^(NSInteger idx) {
        NSString *title = titles[idx];
        [weakSelf updateTitleView:title];
        [weakSelf updateWithTypeIdx:idx];
    };
    
    [self.titleLabel bk_whenTapped:^{
        
        if (![weakSelf.pullView isShowPullView]) {
            [weakSelf.pullView showPullView];
        } else {
            [weakSelf.pullView dismissPullView];
        }
    }];
    
    [self updateTitleView:titles[0]];
    [self updateWithTypeIdx:0];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
}

- (void)updateWithTypeIdx:(NSInteger)idx
{
    switch (idx) {
        case 0:
            _notificationType = DDEmployerNotificationAll;
            break;
        case 1:
            _notificationType = DDEmployerNotificationNewFinder;
            break;
        case 2:
            _notificationType = DDEmployerNotificationJobViewed;
            break;
        case 3:
            _notificationType = DDEmployerNotificationInterested;
            break;
        default:
            break;
    }
    [self reloadData];
}

- (void)updateTitleView:(NSString *)title
{
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:title];
    [titleAttributed addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0,title.length)];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    if ([self.pullView isShowPullView]) {
        textAttachment.image = [UIImage imageNamed:@"pull_up"];
    } else {
        textAttachment.image = [UIImage imageNamed:@"pull_down"];
    }
    
    textAttachment.bounds = CGRectMake(5, 2, 9, 5);
    [titleAttributed insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:title.length];
    
    self.titleLabel.attributedText = titleAttributed;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark --- UITableView DataSourcw

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.notifications.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    DDNotificationMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[DDNotificationMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if (indexPath.row < self.notifications.count)
    {
        DDEmployerNotification *notification = [self.notifications objectAtIndex:indexPath.row];
        [cell setModel:notification];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.notifications.count)
    {
        DDEmployerNotification *notificationModel = [self.notifications objectAtIndex:indexPath.row];
        
//        LDJobfinderDetailBaseVC *vc = [[LDJobfinderDetailBaseVC alloc] init];
//        vc.notiModel = notificationModel;
        
        
        LDJobfinderDetailBaseVCInAll_Show *vc = [[LDJobfinderDetailBaseVCInAll_Show alloc] init];
        //    LDJobfinderDetailBaseVC *vc = [[LDJobfinderDetailBaseVC alloc] init];
        
        vc.userID = notificationModel.jobfinderid;

        
        
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - functions

- (void)reloadData
{
    [self.notifications removeAllObjects];
    self.page = 0;
    [self loadData];
}

- (void)loadMore
{
    self.page += 1;
    [self loadData];
}

- (void)loadData
{
    [self showLoading];
    NSString *url = [[DDAPIGenerate sharedInstance] API:@"employer_query_notifications"];
    __weak typeof(self) weakSelf = self;
    [[DDNetWorkManager shareManager] POST:url parameters:@{@"type":@(_notificationType)} success:^(id responseObject) {
        [weakSelf hideLoading];
        if ([weakSelf isValidateResponseData:responseObject])
        {
            NSDictionary *data = responseObject[@"data"];
            [weakSelf updateWithData:data];
        }
        [weakSelf.tableView reloadData];
    } failure:^(id responseObject, NSError *error) {
        [weakSelf responseUnValidate:responseObject];
    }];
}

- (void)updateWithData:(NSDictionary *)data
{
    self.hasMore = data[@"hasNextPage"];
    if (self.hasMore) {
        NSArray *notifications = data[@"notifications"];
        NSArray *models = [NSArray yy_modelArrayWithClass:[DDEmployerNotification class] json:notifications];
        [self.notifications addObjectsFromArray:models];
        [self.tableView reloadData];
    }
}

- (NSMutableArray *)notifications
{
    if (!_notifications){
        _notifications = [NSMutableArray new];
    }
    return _notifications;
}

@end
